define(['app'], function(app) {
	'use strict';

	//实例化方法
	var factory = function(Http) {
		function get_data_supplierSearch(result) {
			if(false) {
				result = '{"success": true,"msg": "","result":{"data": [' +
					'{"store_id": 1,"store_name": "五金一店","company_profile": "各类五金专卖","company_region": "上海宝山区","company_ctel": "62123456","company_addr": "宝山区上大路","image_place1": "wujinyidian_touxiang.jpg"},' +
					'{"store_id": 2,"store_name": "五金二店","company_profile": "各类五金专卖","company_region": "上海宝山区","company_ctel": "62123456","company_addr": "宝山区上大路","image_place1": "wujinyidian_touxiang.jpg"}]},' +
					'"session": "7311da98bd0f18eb15a96bcb263f4153"}';
				return JSON.parse(result);
			} else {
				return result;
			}
		}

		function get_data_creditList(result) {
			if(false) {
				result = '{"success":true,"msg":"","result":{"credit_line":1000,"debt_total":200,"credit":[{"store_name":"世达","time":"2017/01/01","voucher":[{"id":"001","image":"img/purchaseNow1.png"}]},{"store_name":"世达","time":"2017/01/01","voucher":[{"id":"001","image":"img/purchaseNow1.png"}]}]},"session":"7311da98bd0f18eb15a96bcb263f4153"}';
				return JSON.parse(result);
			} else {
				return result;
			}
		}

		function get_data_agentapply_list(result) {
			if(false) {
				result = '{"success":true,"msg":"","result":{"apply":[{"id":"123456789","apply_time":"2017/02/28","store_id":"001","store_name":"华为制造","brand_id":"001","brand_name":"华为","annual_sales":"500000.00","stimated_sales":"200000.00","dealer_num":15,"brand_count":1,"apply_desc":"主要经营各种手机的销售，批发。"},{"id":"123456789","apply_time":"2017/02/28","store_id":"002","store_name":"华硕制造","brand_id":"002","brand_name":"华硕","annual_sales":"500000.00","stimated_sales":"200000.00","dealer_num":15,"brand_count":1,"apply_desc":"主要经营各种主板的销售，批发。"}]},"session":"7311da98bd0f18eb15a96bcb263f4153"}';
				return JSON.parse(result);
			} else
				return result;

		}
		function get_data_agent_credit(result) {
			if(false) {
				result = '{"success":true,"msg":"","result":{"credit_line":1000,"debt_total":200,"store_name":"世达"},"session":"7311da98bd0f18eb15a96bcb263f4153"}';
				return JSON.parse(result);
			} else {
				return result;
			}
		}
		function get_data_grant(result) {
			if(false) {
				result = '{"success": true,"msg": "","result": {"store_id": "001"},"session": "7311da98bd0f18eb15a96bcb263f4153"}';
				return JSON.parse(result);
			} else
				return result;
		}
		function get_data_product_detail(result){
			if(false){
				result = '{"success":true,"msg":"","result":{"store_name":"五金一店","credit_line":"15000.00","brand_name":"博世","cat_type":[{"name":"气动元件-气抓"},{"name":"输送设备-输送传送带"}],"region":[{"name":"上海-上海"}],"change_price":"可"},"session":"7311da98bd0f18eb15a96bcb263f4153"}';
				return JSON.parse(result);
			}else
			return result;
		}
		function get_data_credit_repay(result) {
			if(false) {
				result = '{"success": true,"msg": "","result": {"store_id": "001"},"session": "7311da98bd0f18eb15a96bcb263f4153"}';
				return JSON.parse(result);
			} else
				return result;
		}
		//返回信息
		return {
			//我的供应商
			supplier_search: function(key, region,nPage, callback,calldone) {
				//post请求
				Http.post({
					method: 'member.supplier.search',
					key: key,
					region: region,
					nPage:nPage,
					pagelimit:10
				}, function(result) {
					//函数回调
					if(callback)
						callback(get_data_supplierSearch(result).result);
				}, function(e) {

				}, function() {
					if(calldone)
					calldone();
				});

			},
			//某一供应商的授信欠款一览
			credit_list: function(store_id,nPage, callback,calldone) {
				Http.post({
					method: 'member.supplier.credit.getlist',
					store_id: store_id,
					pagelimit:10,
					nPage:nPage
				}, function(result) {
					if(callback)
						callback(get_data_creditList(result).result);
				}, function(e) {

				}, function() {
					if(calldone)
					calldone();
				})
			},
			//授信还款
			credit_repay: function(store_id, money,type, image, callback) {
				 var voucher = new Object();
				 voucher.photo = image;
				 voucher.type = type;
				 voucher = JSON.stringify(voucher);
				Http.post({
					method: 'member.supplier.credit.repay',
					store_id: store_id,
					money: money,
					voucher: voucher
				}, function(result) {
					if(callback)
					callback(get_data_credit_repay(result));
				}, function(e) {

				}, function() {

				})
			},
			agentapply_list: function(status,nPage,callback,calldone) {
				Http.post({
					method: 'member.agenterapply.getlist',
					pagelimit: 10,
					nPage:nPage,
					status:status
				}, function(result) {
					if(callback)
						callback(get_data_agentapply_list(result).result);
				}, function(e) {

				}, function() {
					if(calldone)
					calldone();
				})
			},
			agent_credit: function(store_id,  callback) {
				//								//post请求
				Http.post({
					method: 'member.supplier.credit.detail',
					store_id: store_id,
					pagelimit: 8,
					nPage: 1,
					show_count: 5,
				}, function(result) {
					if(callback)
						callback(get_data_agent_credit(result).result);
				}, function(e) {

				}, function() {

				})
			},
			
			//发送红包
			packets_grant: function(count, money, memo, rule,stores, callback) {
				var targets = new Object();
				targets.stores = stores;
				targets = JSON.stringify(targets);
				Http.post({
					method: 'member.packets.grant',
					count: count,
					money: money,
					memo: memo,
					rule: rule,
					type:1,
					targets: targets
				}, function(result) {
					if(callback)
						callback(get_data_grant(result));
				}, function(e) {

				}, function() {

				});
			},
			product_detail:function(store_id,callback){
				Http.post({
					method:'member.supplier.detail',
					store_id:store_id
				},function(result){
					if(callback)
					callback(get_data_product_detail(result).result);
				},function(e){
					
				},function(){
					
				})
			}
		};
	}

	factory.$inject = ['Http'];
	return app.registerService("Mybrandoperator", factory);
});